package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.ContributeInformation;
import com.innovations.inn.service.IContributeInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 投稿栏目信息Controller
 *
 * @author dp
 * @date 2024-11-26
 */
@Api(tags = "投稿栏目信息")
@RestController
@RequestMapping("/inn/contribute/information")
public class ContributeInformationController extends BaseController {
    @Autowired
    private IContributeInformationService contributeInformationService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询投稿栏目信息列表
     */
    @ApiOperation("查询投稿栏目信息列表")
    @PreAuthorize("@ss.hasPermi('inn:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContributeInformation contributeInformation) {
        startPage();
        List<ContributeInformation> list = contributeInformationService.selectContributeInformationList(contributeInformation);
        return getDataTable(list);
    }

    /**
     * 导出投稿栏目信息列表
     */
    @PreAuthorize("@ss.hasPermi('inn:information:export')")
    @Log(title = "投稿栏目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContributeInformation contributeInformation) {
        List<ContributeInformation> list = contributeInformationService.selectContributeInformationList(contributeInformation);
        ExcelUtil<ContributeInformation> util = new ExcelUtil<ContributeInformation>(ContributeInformation.class);
        util.exportExcel(response, list, "投稿栏目信息数据");
    }

    /**
     * 获取投稿栏目信息详细信息
     */
    @ApiOperation("获取投稿栏目信息详细信息")
    @PreAuthorize("@ss.hasPermi('inn:information:query')")
    @GetMapping(value = "/{contributeInformationId}")
    public AjaxResult getInfo(@PathVariable("contributeInformationId") Long contributeInformationId) {
        return success(contributeInformationService.selectContributeInformationByContributeInformationId(contributeInformationId));
    }

    /**
     * 新增投稿栏目信息
     */
    @ApiOperation("新增投稿栏目信息")
    @PreAuthorize("@ss.hasPermi('inn:information:add')")
    @Log(title = "投稿栏目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody ContributeInformation contributeInformation) {
        Long userId = contributeInformation.getCreateBy();
        if (userId == null) {
            userId = tokenService.getLoginUser(request).getUser().getUserId();
        }
        contributeInformationService.completeData(contributeInformation, userId, 1);
        return toAjax(contributeInformationService.insertContributeInformation(contributeInformation));
    }

    /**
     * 修改投稿栏目信息
     */
    @ApiOperation("修改投稿栏目信息")
    @PreAuthorize("@ss.hasPermi('inn:information:edit')")
    @Log(title = "投稿栏目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody ContributeInformation contributeInformation) {
        Long userId = contributeInformation.getCreateBy();
        if (userId == null) {
            userId = tokenService.getLoginUser(request).getUser().getUserId();
        }
        contributeInformationService.completeData(contributeInformation, userId, 2);
        return toAjax(contributeInformationService.updateContributeInformation(contributeInformation));
    }

    /**
     * 删除投稿栏目信息
     */
    @ApiOperation("删除投稿栏目信息")
    @PreAuthorize("@ss.hasPermi('inn:information:remove')")
    @Log(title = "投稿栏目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{contributeInformationIds}")
    public AjaxResult remove(@PathVariable Long[] contributeInformationIds) {
        return toAjax(contributeInformationService.deleteContributeInformationByContributeInformationIds(contributeInformationIds));
    }
}
